package com.malou.client.ventas.cajainicial;

import java.util.Date;

import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.malou.client.MalouConstants;
import com.malou.client.util.MensajeData;

public class CajaIniciales {
	
	private static CajaInicialServiceAsync si = GWT.create(CajaInicialService.class);
	private static MalouConstants constants = GWT.create(MalouConstants.class);
	public static CajaInicialForm cif;	
	private static Date fecha=new Date();

	
	public static void checkSaldoInicial(){
		
		si.getCajaInicial(fecha, new AsyncCallback<Double>() {
			
			@Override
			public void onSuccess(Double result) {
				cif.setMontoCaja(result);
							}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert(constants.sistemaMarcocruzado(), constants.errorConexion(), null);
			}
		});
	}
	
	public static void save(final CajaInicial ci) {
		
		
		si.save(ci, new AsyncCallback<MensajeData>() {

			@Override
			public void onFailure(Throwable caught) {				
				MessageBox.alert(constants.sistemaMarcocruzado(), constants.errorConexion(), null);			
			}

			@Override
			public void onSuccess(MensajeData data) {
				if (data.guardado) {
					checkSaldoInicial();
					cif.reset();
				}
				 else
					 MessageBox.alert(constants.sistemaMarcocruzado(), constants.cajainicialNoGuardado(), null);
					 
			}
			
		});
	}
	
	
}
